package com.handong.handserialprogram.module.qrscan.update;

import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;

import java.util.List;

public class UpdateRingAdapter extends RecyclerView.Adapter<UpdateRingAdapter.UpdateRingHolder> {

    private List<FootRingNewEntity> mFootRingList;
    private CallBack back;
    public UpdateRingAdapter(List<FootRingNewEntity> list,CallBack callBack) {
        mFootRingList = list;
        back=callBack;
    }
    public void SetRingList(List<FootRingNewEntity> list)
    {
        mFootRingList=list;
    }
    @Override
    public UpdateRingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_ring_update_item, parent, false);
        return new UpdateRingHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final UpdateRingHolder holder, int position) {
        final FootRingNewEntity entity = mFootRingList.get(position);
        holder.ringID.setText(entity.getRingID());
        holder.ringVersion.setText("当前版本："+entity.getVersion());
        if (holder.aSwitch.isChecked()){
            back.getPosition(position);
        }
        holder.aSwitch.setClickable(false);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back.doCheckAll(holder.aSwitch,entity.getVersion());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFootRingList == null ? 0 : mFootRingList.size();
    }

    public class UpdateRingHolder extends RecyclerView.ViewHolder {
        public TextView ringID, ringVersion;
        public Switch aSwitch;
        public UpdateRingHolder(View itemView) {
            super(itemView);
            ringID = (TextView) itemView.findViewById(R.id.ringItemID);
            ringVersion=(TextView) itemView.findViewById(R.id.ringVersion);
            aSwitch=itemView.findViewById(R.id.switch_update);
        }
    }
    interface CallBack{
        void doCheckAll(CompoundButton buttonView,String version);
        void getPosition(int integer);
    }
}
